package com.sf.osms.dep.biz.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sf.osms.dep.biz.ICommonServiceBiz;
import com.sf.osms.dep.dao.IAgentConfigDao;
import com.sf.osms.dep.dao.IAgentDao;
import com.sf.osms.dep.dao.IAgentFileMapDao;
import com.sf.osms.dep.dao.IChargeBaseDao;
import com.sf.osms.dep.dao.IRouterDao;
import com.sf.osms.dep.dao.IRouterProblemRecordDao;
import com.sf.osms.dep.dao.IRouterTmpDao;
import com.sf.osms.dep.dao.ISfCodeDao;
import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.domain.AgentConfig;
import com.sf.osms.dep.domain.AgentFileMap;
import com.sf.osms.dep.domain.ChargeBase;
import com.sf.osms.dep.domain.Router;
import com.sf.osms.dep.domain.RouterProblemRecord;
import com.sf.osms.dep.domain.RouterTmp;
import com.sf.osms.dep.domain.SfCode;
import com.sf.osms.dep.processor.util.CollectionUtil;

@Service("commonRouteService")
public class ICommonServiceBizImpl implements ICommonServiceBiz{

	@Autowired
	private IAgentDao agentDao;
	
	@Autowired
	private IAgentConfigDao agentConfigDao;
	
	@Autowired
	private IAgentFileMapDao agentFileMapDao;
	
	@Autowired
	private IRouterDao routerDao;
	
	@Autowired
	private IRouterProblemRecordDao routerProblemDao;
	
	@Autowired
	private Mapper mapper;
	
	@Autowired
	private ISfCodeDao sfCodeDao;
	
	@Autowired
	private IRouterTmpDao routerTmpDao;
	
	@Autowired
	private IChargeBaseDao chargeBaseDao;
	

	@Override
	public Agent findAgentInfoById(Long agentId) {
		
		if(null == agentId){
			return null;
		}
		
		Agent agentQuery = new Agent();
		agentQuery.setId(agentId);
		List<Agent> agents = agentDao.findByBean(agentQuery);
		if(CollectionUtil.isEmpty(agents)){
			return null;
		}
		
		//agent
		Agent agentRtn = agents.get(0);
		
		//agentFileMap
		AgentFileMap afmQuery = new AgentFileMap();
		afmQuery.setAgentId(agentId);
		afmQuery.setUseFlag("1"); //设置使用的默认值
		List<AgentFileMap> agentFileMaps = agentFileMapDao.findByBean(afmQuery);
		if(!CollectionUtil.isEmpty(agentFileMaps)){
			agentRtn.setAgentFileMap(agentFileMaps.get(0));
		}
		
		//agentconfig
		AgentConfig acQuery = new AgentConfig();
		acQuery.setAgentId(agentId);
		List<AgentConfig> agentConfigs = agentConfigDao.findByBean(acQuery);
		agentRtn.setAgentCofigs(agentConfigs);
		
		
		return agentRtn;
	}

	@Override
	public List<Agent> findAllAgentInfo() {
		
			List<Agent> agents = agentDao.findAll();
			//对isDeleted为1的agent进行过滤
			/*Iterator<Agent> iterator = agents.iterator();
			while (iterator.hasNext()) {
				Agent agent = iterator.next();
				if(1 == agent.getIsDeleted()){
					iterator.remove();
				}
			}*/
			
			if(CollectionUtil.isEmpty(agents)){
				return null;
			}
			
			for (Agent agent : agents) {
			
				
				//agentFileMap
				AgentFileMap afmQuery = new AgentFileMap();
				afmQuery.setAgentId(agent.getId());
				afmQuery.setUseFlag("1"); //设置使用的默认值
				List<AgentFileMap> agentFileMaps = agentFileMapDao.findByBean(afmQuery);
				if(!CollectionUtil.isEmpty(agentFileMaps)){
					agent.setAgentFileMap(agentFileMaps.get(0));
				}
				
				//agentconfig
				AgentConfig acQuery = new AgentConfig();
				acQuery.setAgentId(agent.getId());
				List<AgentConfig> agentConfigs = agentConfigDao.findByBean(acQuery);
				
				if(!CollectionUtil.isEmpty(agentConfigs)){
					agent.setAgentCofigs(agentConfigs);
				}
				
				
			}
			
			return agents;
		
	}

	@Override
	public List<Router> findRouterInfo(String sfWayfill,Integer eventCode, boolean failed) throws Exception {
		List<Router> routers = routerDao.findRouterInfo(sfWayfill,eventCode, failed);
		return routers;
	}

	@Override
	public void insertRouter(Router router) throws Exception {
		if(null == router){
			return;
		}
		
		routerDao.save(router);
	}

	@Override
	@Transactional
	public void insertRouterBatch(Collection<Router> routers) throws Exception {
		routerDao.saveBatch(routers);
	}
	
	@Override
	@Transactional
	public void removeRouterBatch(Collection<Router> routers) throws Exception {
		routerDao.removeBatch(routers);
	}

	@Override
	public void insertRouterProblem(RouterProblemRecord record)
			throws Exception {
		if(null == record){
			return;
		}
		
		record.setCreateTime(new Date());
		
		routerProblemDao.save(record);
	}

	@Override
	public void insertRouterProblemBatch(List<RouterProblemRecord> records)
			throws Exception {
		if(CollectionUtil.isEmpty(records)){
			return;
		}
		
		routerProblemDao.saveBatch(records);
		
	}

	@Override
	public <T> T mapBean(Object source, Class<T> dest) throws Exception {
		return mapper.map(source, dest);
	}

	@Override
	public List<Router> queryRouterByStatus(int status) {
		return routerDao.queryRouterByStatus(status);
	}

	@Override
	public void updateRoute(List<Router> router) {
		routerDao.updateBatch(router);;
	}

	@Override
	public List<String> findAllSfCode() {
		List<SfCode> sfCodes = sfCodeDao.findAll();
		List<String> sfCodesStr = new ArrayList<String>();
		for (SfCode sfCode : sfCodes) {
			String str = sfCode.getEventCode().trim();
			if(StringUtils.isNotBlank(sfCode.getReasonCode())){
				str = str + "," + sfCode.getReasonCode().trim();
			}
			sfCodesStr.add(str);
		}
		return sfCodesStr;
	}

	@Override
	public void insertRouterTmp(Collection<RouterTmp> routerTmps) {
		routerTmpDao.saveBatch(routerTmps);
	}

	@Override
	public List<Router> getRouterInfo(Router router) {
		
		return routerDao.findByBean(router);
	}

	@Override
	public List<Router> queryRouterNeedPod(String country) {
		
		return routerDao.queryRouterNeedPod(country);
	}

	@Override
	public Set<String> queryAgentCountry() {
		List<Agent> agents = agentDao.findAll();
		Set<String> agentCountries = new HashSet<String>();
		for (Agent agent : agents) {
			agentCountries.add(agent.getCountry());
		}
		return agentCountries;
	}

	@Override
	public Map<String, ChargeBase> queryHandOverInfo(Set<String> sfWaybillNos) {
		
		return chargeBaseDao.queryPodHandOverData(sfWaybillNos);
	}

	@Override
	public Agent findAgentInfo(String name) {
		
		Agent agentQuery = new Agent();
		agentQuery.setName(name);
		List<Agent> agents = agentDao.findByBean(agentQuery);
		if(CollectionUtil.isEmpty(agents)){
			return null;
		}
		
		//agent
		Agent agentRtn = agents.get(0);
		
		//agentFileMap
		AgentFileMap afmQuery = new AgentFileMap();
		afmQuery.setAgentId(agentRtn.getId());
		afmQuery.setUseFlag("1"); //设置使用的默认值
		List<AgentFileMap> agentFileMaps = agentFileMapDao.findByBean(afmQuery);
		if(!CollectionUtil.isEmpty(agentFileMaps)){
			agentRtn.setAgentFileMap(agentFileMaps.get(0));
		}
		
		//agentconfig
		AgentConfig acQuery = new AgentConfig();
		acQuery.setAgentId(agentRtn.getId());
		List<AgentConfig> agentConfigs = agentConfigDao.findByBean(acQuery);
		agentRtn.setAgentCofigs(agentConfigs);
		
		
		return agentRtn;
	}

	
}
